# coding = utf-8
import os

from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import SimpleITK as sitk
import cv2

def vis_data():
   label_path = "E:\predict\image_tumor_v1\case_00073\label_tumor"
   predict_path = "E:\predict\image_tumor_v1\case_00073\predict_tumor"

   name_list = ["093.png", "094.png", "095.png", "096.png"]

   label = []
   predict = []

   for item in name_list:
       label_file = os.path.join(label_path, item)
       label_data = Image.open(label_file).convert("L")
       label_data = np.array(label_data)
       label.append(label_data)
       predict_file = os.path.join(predict_path, item)
       predict_data = Image.open(predict_file).convert("L")
       predict_data = np.array(predict_data)
       predict.append(predict_data)

   plt.subplot(2, 4, 1)
   plt.imshow(label[0],cmap="gray")
   plt.subplot(2, 4, 2)
   plt.imshow(label[1], cmap="gray")
   plt.subplot(2, 4, 3)
   plt.imshow(label[2], cmap="gray")
   plt.subplot(2, 4, 4)
   plt.imshow(label[3], cmap="gray")
   plt.subplot(2, 4, 5)
   plt.imshow(predict[0], cmap="gray")
   plt.subplot(2, 4, 6)
   plt.imshow(predict[1], cmap="gray")
   plt.subplot(2, 4, 7)
   plt.imshow(predict[2], cmap="gray")
   plt.subplot(2, 4, 8)
   plt.imshow(predict[3], cmap="gray")
   plt.show()


def read_artery_vein():
    venous = "E:\Dataset\Liver\qiye\DongBeiDaXue2\image_venous\\data2_0628_venous.mha"
    artery = "E:\Dataset\Liver\qiye\DongBeiDaXue2\image_arterial\\data2_0628_arterial.mha"

    arterial_image = sitk.ReadImage(artery)
    arterial = sitk.GetArrayFromImage(arterial_image)

    venous_image = sitk.ReadImage(venous)
    venous = sitk.GetArrayFromImage(venous_image)

    arterial[arterial <= -250] = -250
    arterial[arterial > 250] = 250

    venous[venous <= -250] = -250
    venous[venous > 250] = 250

    arterial = arterial[140]
    venous = venous[140]

    liver_path = "E:\predict\image_tumor\case_00072\predict_liver\\140.png"
    liver = Image.open(liver_path).convert("L")
    liver = np.array(liver)
    liver = liver/255
    liver = liver.astype(np.uint8)


    arterial = (arterial+250)/500
    arterial = arterial * 255
    arterial = arterial.astype(np.uint8)
    arterial = cv2.cvtColor(arterial, cv2.COLOR_GRAY2BGR)
    venous = (venous+250)/500
    venous = venous * 255
    venous = venous.astype(np.uint8)

    venous_v2 = venous
    venous_v2 = cv2.equalizeHist(venous_v2)
    venous_v2 = cv2.cvtColor(venous_v2, cv2.COLOR_GRAY2BGR)

    venous = cv2.cvtColor(venous, cv2.COLOR_GRAY2BGR)


    tumor_label_file = "E:\predict\image_tumor\case_00072\label_tumor\\140.png"
    tumor_label = Image.open(tumor_label_file).convert("L")
    tumor_label = np.array(tumor_label)
    label_copy = tumor_label.astype(np.uint8)
    contours, _ = cv2.findContours(label_copy, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for counter in contours:
        data_list = []
        for t in range(counter.shape[0]):
            j = counter[t][0]
            data_list.append(j)
        cv2.polylines(venous, np.array([data_list], np.int32), True, [0, 255, 0], thickness=1)
        cv2.polylines(arterial, np.array([data_list], np.int32), True, [0, 255, 0], thickness=1)

    print(arterial.shape, venous.shape)

    plt.subplot(1, 2, 1)
    plt.imshow(venous)
    plt.subplot(1, 2, 2)
    plt.imshow(arterial)
    #plt.subplot(1, 3, 3)
    #plt.imshow(arterial)
    plt.show()



if __name__ == '__main__':
    read_artery_vein()